﻿using System;
using System.Collections.Generic;
using Summer.Object;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Core.Ioc
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-03 15:27:06
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Core.Ioc
{
    /// <summary>
    /// All of the ioc container should inherit it
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class AbstractContextContainer<T>:IContextContainer<T>, IExtensionDictionary<string,T>
        where T : IContextObject
    {        
        /// <summary>
        /// Container name
        /// </summary>
        private string m_name = "AbstractContextContainer";
        /// <summary>
        /// Whether the contextObject is singletion
        /// </summary>
        private bool m_singleton = true;
        /// <summary>
        /// The path of class that can create instance
        /// </summary>
        private string m_class;
        /// <summary>
        /// The dictionary collection
        /// </summary>
        private IExtensionDictionary<string, T> m_dictionary = new ExtensionDictionary<string, T,object>();
        /// <summary>
        /// The additional object used when object is added into m_dictionary
        /// </summary>
        private object m_extension=null;

        public AbstractContextContainer()
        {

        }
        public AbstractContextContainer(object o)
        {
            this.m_extension = o;
        }

        /// <summary>
        /// When the object is added into another collection, the load function would be run
        /// </summary>
        /// <returns></returns>
        public virtual bool Load(object additional)
        {
            return true;
        }
        /// <summary>
        /// Get or set Container's name
        /// </summary>
        /// <returns></returns>
        public virtual string Name
        {
            get { return this.m_name; }
            set { this.m_name = value; }
        }
        /// <summary>
        /// When the object is removed from another collection,the unload function would be run
        /// </summary>
        /// <returns></returns>
        public virtual bool UnLoad()
        {
            return true;
        }

        #region IDisposable 成员

        void IDisposable.Dispose()
        {
            foreach (var obj in this.m_dictionary)
            {
                obj.Value.UnLoad();
            }
        }

        #endregion

        #region IExtensionObject<object> 成员

        bool Summer.Object.IExtensionObject<object>.Load(object additional)
        {
            return this.Load(additional);
        }

        bool Summer.Object.IExtensionObject<object>.UnLoad()
        {
            return this.UnLoad();
        }

        #endregion

        #region IDictionary<string,T> 成员

        public void Add(string key, T value)
        {
            value.Load(m_extension);
            this.m_dictionary.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return this.m_dictionary.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return this.m_dictionary.Keys; }
        }

        public bool Remove(string key)
        {
            return this.m_dictionary.Remove(key);
        }

        public bool TryGetValue(string key, out T value)
        {
            return this.m_dictionary.TryGetValue(key, out value);
        }

        public ICollection<T> Values
        {
            get { return this.m_dictionary.Values; }
        }

        public T this[string key]
        {
            get
            {
                return this.m_dictionary[key];
            }
            set
            {
                this.m_dictionary[key] = value;
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,T>> 成员

        public void Add(KeyValuePair<string, T> item)
        {
            this.m_dictionary.Add(item);
        }

        public void Clear()
        {
            this.m_dictionary.Clear();
        }

        public bool Contains(KeyValuePair<string, T> item)
        {
            return this.m_dictionary.Contains(item);
        }

        public void CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
        {
            this.m_dictionary.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.m_dictionary.Count; }
        }

        public bool IsReadOnly
        {
            get { return this.m_dictionary.IsReadOnly; }
        }

        public bool Remove(KeyValuePair<string, T> item)
        {
            return this.m_dictionary.Remove(item);
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,T>> 成员

        public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
        {
            return this.m_dictionary.GetEnumerator();
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion

        #region IContextObject 成员

        string IContextObject.Name
        {
            get
            {
                return this.Name;
            }
            set
            {
                this.Name = value;
            }
        }

        public object Object
        {
            get
            {
                return m_extension;
            }
            set
            {
                m_extension = value;
            }
        }

        public IContextObjectProperty[] Property
        {
            get
            {
                return null;
            }
            set
            {
                
            }
        }

        public bool Singleton
        {
            get
            {
                return m_singleton;
            }
            set
            {
               
            }
        }

        #endregion

        #region IContextObject 成员


        public string Class
        {
            get
            {
                return m_class;
            }
            set
            {
                m_class = value;
            }
        }

        public string Ref
        {
            get
            {
                return null;
            }
            set
            {
                
            }
        }

        #endregion
    }
}
